home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / post1.0 / ui / example-swing / RadioButtonDemo.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  3.5 KB  |  109 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.applet.Applet;
  18. import java.awt.*;
  19. import java.awt.event.*;
  20. import com.sun.java.swing.JRadioButton;
  21. import com.sun.java.swing.ButtonGroup;
  22. import com.sun.java.swing.JPanel;
  23. import com.sun.java.swing.JFrame;
  24. import com.sun.java.swing.ChangeListener;
  25. import com.sun.java.swing.ChangeEvent;
  26.  
  27. /**
  28.  * An application that displays two JRadioButtons.  The JRadioButtons
  29.  * determine the look and feel used by the application.
  30.  */
  31. public class RadioButtonDemo extends JPanel {
  32.     static JFrame frame;
  33.     static String first = new String("Button 1");
  34.     static String second = new String("Button 2");
  35.  
  36.     public RadioButtonDemo() {
  37.     super(true);
  38.  
  39.     // Create the buttons.
  40.     JRadioButton firstButton = new JRadioButton(first);
  41.         firstButton.setKeyAccelerator('1'); 
  42.     firstButton.setActionCommand(first);
  43.     firstButton.setSelected(true);
  44.  
  45.     JRadioButton secondButton = new JRadioButton(second);
  46.         secondButton.setKeyAccelerator('2'); 
  47.     secondButton.setActionCommand(second);
  48.  
  49.     // Group the radio buttons.
  50.     ButtonGroup group = new ButtonGroup();
  51.     group.add(firstButton);
  52.     group.add(secondButton);
  53.  
  54.         // Register a listener for the radio buttons.
  55.     RadioListener myListener = new RadioListener();
  56.     firstButton.addActionListener(myListener);
  57.     firstButton.addChangeListener(myListener);
  58.     firstButton.addItemListener(myListener);
  59.     secondButton.addActionListener(myListener);
  60.     secondButton.addChangeListener(myListener);
  61.     secondButton.addItemListener(myListener);
  62.  
  63.     add(firstButton);
  64.     add(secondButton);
  65.     }
  66.  
  67.  
  68.     /** Listens to the radio buttons. */
  69.     class RadioListener implements ActionListener, //only one event type needed
  70.                    ChangeListener, //for curiosity only
  71.                    ItemListener {  //for curiosity only
  72.     public void actionPerformed(ActionEvent e) {
  73.         String factoryName = null;
  74.  
  75.         System.out.print("ActionEvent received: ");
  76.         if (e.getActionCommand() == first) {
  77.         System.out.println(first + " pressed.");
  78.         } else {
  79.         System.out.println(second + " pressed.");
  80.         }
  81.     }
  82.  
  83.     public void itemStateChanged(ItemEvent e) {
  84.         System.out.println("ItemEvent received: " 
  85.                    + e.getItem()
  86.                    + " is now "
  87.                    + ((e.getStateChange() == ItemEvent.SELECTED)?
  88.                    "selected.":"unselected"));
  89.     }
  90.  
  91.     public void stateChanged(ChangeEvent e) {
  92.         System.out.println("ChangeEvent received from: "
  93.                    + e.getSource());
  94.     }
  95.     }
  96.  
  97.     public static void main(String s[]) {
  98.          WindowListener l = new WindowAdapter() {
  99.              public void windowClosing(WindowEvent e) {System.exit(0);}
  100.          };
  101.  
  102.          frame = new JFrame("RadioButtonDemo");
  103.          frame.addWindowListener(l);
  104.          frame.add("Center", new RadioButtonDemo());
  105.          frame.pack();
  106.          frame.setVisible(true);
  107.     }
  108. }
  109.